<template>
    <div class="container index-page">

        <!-- <div class="student">
            <el-input placeholder="请选择学生" icon="search" v-model="student" class="input" @focus="componentsShow('student')"></el-input>
            <el-button type="text">缴费记录</el-button>
        </div>

        <div class="btn">
            <el-button type="info" @click="componentsShow('course')">购买课时</el-button>
            <el-button type="info" @click="componentsShow('article')">购买物品</el-button>
        </div> -->
        <i class="el-icon-close close-top" @click="cancel"></i>
        <el-button type="success" icon="d-arrow-left" @click="cancel" size="small">返回</el-button>
        <div class="name">
            <span>学生：</span>
            <el-input  v-model="name" :readonly="true" class="small-input"></el-input>

        </div>

        <!-- 数据表格展示区域 -->
        <!-- <el-table
            :data="dataList"
            border
            align="center"
            class="admin-table"
            sum-text
            :row-class-name="tableRowClassName"
            @select="checkSelect"
            @select-all="checkall">

            <el-table-column
                :prop="v.prop"
                :label="v.label"
                v-for="(v,index) in portTitleList"
                align="center"
                show-overflow-tooltip
                v-if="v.hide != 0"
                :formatter="v.formatter"
                :align="v.align || 'center'"
                :fixed="v.fixed"
                :class-name="v.columnStyle"
                :width="v.width"
                :min-width="v.minWidth"
                :header-align="v.headerAlign || 'center'"
                >
                <template scope="scope">
                    <div slot="reference" :style="tableCellControl(scope)" v-if="scope.column.property === 'number' ||  scope.column.property === 'zk'">
                        <el-input class="content-input" v-model="scope.row[scope.column.property]"></el-input>
                    </div>
                    <div slot="reference" v-else-if="scope.column.property === 'yj'">
                        {{scope.row.jcjg * scope.row.number * scope.row.zk.replace(/%/,"")*1/100 || 0}}
                    </div>
                    <div slot="reference" v-else>
                       {{ scope.row[scope.column.property] }}
                    </div>
                 </template>
            </el-table-column> -->
            <el-table
                :data="dataList"
                :v-loading="false"
                element-loading-text="拼命加载中"
                border
                stripe
                align="center"
                class="admin-table"
                :row-class-name="tableRowClassName">


                <el-table-column
                    :prop="v.prop"
                    :label="v.label"
                    v-for="(v,index) in portTitleList"
                    align="center"
                    show-overflow-tooltip
                    v-if="v.hide != 0"
                    :formatter="v.formatter"
                    :align="v.align || 'center'"
                    :fixed="v.fixed"
                    :class-name="v.columnStyle"
                    :width="v.width"
                    :min-width="v.minWidth"
                    :header-align="v.headerAlign || 'center'"
                    >
                </el-table-column>
            </el-table>

        </el-table>

        <div class="total">
            <span>课程：￥{{courseTotal}}</span>
            <span>物品：￥{{goodsTotal}}</span>
            <!-- <span>优惠金：￥{{oldTicket}}</span> -->
            <span>合计：￥{{totalAmount}}</span>
        </div>
        <article class="settlement">
            <p>结算</p>
            <div>
                代金券抵扣：<input type="text" v-model="oldTicket" disabled>
                <span class="space-between"></span>
                已收金额：<input type="text" v-model="amountReceived" disabled>
                <span class="space-between"></span>
                需交金额：<input type="text" v-model="xujiao">
            </div>
        </article>

        <article class="receiving-account">
            <p>收款账户</p>
            <div>
                现金：<input type="text" v-model="money.xj">
                <span class="space-between"></span>
                支付宝账号：<input type="text" v-model="money.zfbzh">
                金额：<input type="text" v-model="money.zfbzhje">
                <span class="space-between"></span>
                微信账号：<input type="text" v-model="money.wxzh">
                金额：<input type="text" v-model="money.wxzhje">
                <span class="space-between"></span>
                银行卡账号：<input type="text" v-model="money.yhzh">
                金额：<input type="text" v-model="money.yhzhje">
                <!-- <span class="space-between"></span>
                总续费金额：<input type="text" v-model="xfje"> -->
            </div>
        </article>

        <!-- <article class="other-information">
            <p>其他信息</p>
            <div>
                购买日期：<el-date-picker type="date" placeholder="请选择日期" v-model="date" class="item-input" @change="getTime"></el-date-picker>
            </div>
        </article> -->
        <el-button type="info" class="submit" @click="submit">续交</el-button>


        <!-- 各个组件显示区域 -->

        <!-- 动画加载 -->
        <LoadingComponent v-show="isLoading"></LoadingComponent>



        <!-- 子路由显示区域 -->
        <router-view class="port-view"></router-view>
    </div>
</template>

<script>
    import Vue from 'vue'
    import { mapState } from 'vuex'
    import LoadingComponent from 'components/loading'

    export default {
        props: ['params'],
        components: {
            LoadingComponent
        },
        data () {
            return {
                name: '',
                student: '',
                studentList: [],
                date: '',
                oldTicket: 0,
                ticket: 10,
                sum: 0,
                amountReceived: 0,
                needPay: 10,

                money: {
                    xj: '',
                    zfbzh: '',
                    zfbzhje: '',
                    wxzh: '',
                    wxzhje: '',
                    yhzh: '',
                    yhzhje: ''
                },
                /*组件区域*/
                portComponents: {
                    singleDelete: {
                        type: 'singleDelete',
                        toggle: false,
                        params: '',
                        data: {
                            index: '',
                            type: 'single',
                            title: '此操作将删除该班级, 是否继续?'
                        }
                    },
                    multipleDelete: {
                        type: 'multipleDelete',
                        toggle: false,
                        params: '',
                        data: {
                            index: '',
                            type: 'multiple',
                            title: '此操作将删除您选中的班级, 是否继续?'
                        }
                    },
                    student : {
                        type: 'student',
                        toggle: false,
                        params: '',
                        data: {}
                    },
                    course : {
                        type: 'course',
                        toggle: false,
                        params: '',
                        data: {}
                    },
                    article : {
                        type: 'article',
                        toggle: false,
                        params: '',
                        data: {}
                    }
                },

                multipleDelNum:'', //获取批量选择的数据
                dataList: [],  //表格数据

                /*
                    prop :  接口参数名
                    label : 接口中文名
                    hide : 是否隐藏这一列
                    align : 这一列对齐顺序 ： left,right,center
                    headerAlign : 这一列表头对齐方式 ： left,right,center
                    formatter: function (row, column) {}
                */
                portTitleList: [
                    {
                        prop:'id',
                        label:'id',
                        hide:0,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    },{
                        prop:'splx',
                        label:'商品类型',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {
                            // console.log(row, column)
                            return row.splx == 1 ? '课时' : '物品'
                        }
                    },{
                        prop:'spmc',
                        label:'商品名称',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },{
                        prop:'jcjg',
                        label:'基础价格',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },{
                        prop:'sl',
                        label:'数量',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },{
                        prop:'dw',
                        label:'单位',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {
                            return row.splx == 1 ? '次数' : row.dw ? row.dw : '套'
                        }
                    },
                    // {
                    //     prop:'yhj',
                    //     label:'优惠金额（-）',
                    //     hide:1,
                    //     align:'center',
                    //     fixed:false,
                    //     width:'',
                    //     minWidth:'',
                    //     headerAlign:'center'
                    // },
                    {
                        prop:'zk',
                        label:'折扣',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {
                            return row.zk + "%"
                        }
                    }

                ],
                portBntList: [//接口按钮列表
                    {
                        name:'删除',
                        type:'singleDelete'
                    }
                ],

                /*动画加载*/
                isLoading: true,//动画加载
                searchLoading: false,//搜索按钮触发按钮动画

                /*模糊搜索条件*/
                inputbjmc:'' , //  班级名称,


                /*分页属性设置*/
                layout: 'total, sizes, prev, pager, next, jumper',//组件布局
                pageindex: 1,//当前分页页码
                pagesizes: [12, 24, 36, 72],//显示个数的选择器选项
                pagesize: 12,//每页显示条目数
                total: 0,//总条数


                dropDownData:'', //
            }
        },
        created () {
                // console.log(this.params)
            this.name = this.params.row.zwm;
            this.id = this.params.row.id;
            this.yuer = this.params.row.yuer;
            //加载页面时调用
            this.$store.dispatch('changeTitle','续交');//修改title
            this.$store.dispatch('changeRouterIndex','/admin/yushebj');//修改路由
            this.search()
            this.isLoading = true;//动画开始

        },
        mounted () {
            this.isLoading = false;//动画结束
        },
        methods: {
            tableRowClassName (row, index) {//隔行变色颜色设置
                /*if ( index%4 == 0) {
                    return "table-cell-green";
                }else if( index%4 == 1 ){
                    return "table-cell-yellow";
                }else if( index%4 == 2 ){
                    return "table-cell-red";
                }else{
                    return "table-cell-blue"
                }*/
            },
            tableCellControl (scope) {//表格单元格控制CSS样式
                // console.log(scope)
                /*if (scope.row.id == 112 && scope.column.property == "iname") {
                    return {
                        color:"red",
                    }
                }else{
                    return {
                        color:"green",
                    }
                }*/
            },

            // getTime () {
            //
            // },

            submit () {
              let promise = new Promise( (resolve, reject) => {
                  var logininfo = JSON.parse(localStorage.logininfo) ;

                  this.axios.get(this.$store.state.api + 'order/insert', {
                      params: {
                          xsid:this.id,
                          yjje:this.totalAmount,
                          // je:this.amountReceived,
                          yhj:this.oldTicket,
                          xj:this.money.xj,
                          zfbzh:this.money.zfbzh,
                          zfbje:this.money.zfbzhje,
                          wxzh:this.money.wxzh,
                          wxje:this.money.wxzhje,
                          yhkzh:this.money.yhzh,
                          yhkje:this.money.yhzhje,
                          jfr:this.id,
                          je:this.xfje,
                          jsr:logininfo.id,
                            // je:this.amountReceived
                      }
                  }).then( res => {
                      console.log(res)
                      console.log(123)
                      resolve(res.data)
                      // location.reload() //刷新页面
                      this.dialogAccounts = false;
                  }).catch( res => {
                      console.log(res)
                  })
              })

              promise.then( res => {
                  console.log(res)
                  console.log(211)
                  this.dataList.forEach( (v, i) => {
                      this.axios.get(this.$store.state.api + 'ordermx/insert', {
                          params: {
                              orderid:res,
                              splx: v.type == '物品' ? 2 : 1,
                              spid: v.id,
                              jg: v.jg,
                              zk: v.zk,
                              cns: v.sl
                          }
                      }).then( res => {
                          console.log(res)
                      }).catch( res => {
                          console.log(res)
                      })
                  })
              }).catch( res => {
                  console.log(res)
              })

              var jcid = [],
                  zys = 0, //总验收
                  zss = this.courseTotal - this.ticket; //总实收
              this.dataList.forEach( v => {
                  // console.log(v)
                  if (v.type === '课时') {
                      jcid.push(v.id);
                      zys += v.jcjg;
                  }
              })
              var logininfo = JSON.parse(localStorage.logininfo);
        				this.axios.get( this.$store.state.api + 'xs/updateyuer',{
        					params:{
                    yjje:this.totalAmount,
        						yhid: logininfo.id,
        						xxid:logininfo.xxid,
                    id: this.id
        					}
        				})
        				.then( res => {
        						console.log(res)
        				}).catch( res => {
        						console.log(res)
        				})
                this.$emit('close',{type:'bujiao',cancel:true})
            },

            search () {

                this.getArticle()

            },

            getArticle () {
                var logininfo = JSON.parse(localStorage.logininfo) ;
                this.axios.get(this.$store.state.api + 'ysbj/kcmx',{
                    params:{
                        xsid: this.id,
                        yhid:logininfo.id,
                        xxid:logininfo.xxid,
                    }
                }).then( res => {
                    console.log(res)

                    let list = res.data.data;
                    // this.total = data.totalPage;
                    // console.log(111)
                    var arr = []
                    list.forEach(v => {
                        if (v.splx) {
                            arr.push(v)
                        }
                    })

                    this.dataList = this.dataList.concat(arr);
                    this.amountReceived = this.dataList[0].je ?  this.dataList[0].je : this.amountReceived;
                    this.amountReceived = this.yuer ?  this.yuer : this.amountReceived;
                    // this.oldTicket = this.dataList[0].yhj
                    // this.totalAmount = this.dataList[0].je ?  this.dataList[0].je : this.totalAmount;



                    this.searchLoading = false;
                }).catch( res => {
                    console.log(res)
                    this.searchLoading = false;
                })


                // this.axios.get(this.$store.state.api + 'order/listks',{
                //     params:{
                //         orderid: this.params.row.orderid
                //     }
                // }).then( res => {
                //     console.log(res)
                //
                //     let list = res.data.data.list;
                //     var arr = [];
                //     list.forEach(v => {
                //         if (v.splx) {
                //             arr.push(v)
                //         }
                //     })
                //     console.log(123)
                //     this.dataList = this.dataList.concat(arr);
                //     this.amountReceived = this.dataList[0].je ?  this.dataList[0].je : this.amountReceived;
                //     // this.oldTicket = this.dataList[0].yhj
                //     // console.log(this.oldTicket)
                //     // this.totalAmount = this.dataList[0].je ?  this.dataList[0].je : this.totalAmount;
                //
                //     this.searchLoading = false;
                // }).catch( res => {
                //     console.log(res)
                //     this.searchLoading = false;
                // })

            },



            checkSelect (selection, row) {//点击单选select触发事件
                console.log(selection, row)
                let strID = [];
                selection.forEach(function(v,i){
                    strID.push(v.id)
                })
                this.multipleDelNum = strID.join(",");
            },
            checkall (selection) {//点击全选select触发事件
                // console.log(selection)
                let strID = [];

                selection.forEach(function(v,i){
                    strID.push(v.id)
                })
                this.multipleDelNum = strID.join(",");
            },


            cancel () {
               this.$emit('close',{type:'bujiao',cancel:true})
            },




            /* 组件的弹框  */
            componentsShow (type,data) { //显示组件
                console.log(type,data)
                for ( let i in this.portComponents ) {
                    if (type == this.portComponents[i].type) {
                        console.log(this.portComponents[i])

                        this.portComponents[i].toggle = true;
                        this.portComponents[i].params = data;

                        if (this.portComponents[i].data.type == 'single') {
                            this.portComponents[i].data.index = data.row.id

                        }else if(this.portComponents[i].data.type == 'multiple'){
                            this.portComponents[i].data.index = this.multipleDelNum;

                        }else{
                            return;
                        }
                        return;
                    }
                }

            },
            componentsHide (setting) { //关闭组件
                console.log(setting)
                for ( let i in this.portComponents ) {
                    if (setting.type == this.portComponents[i].type) {
                        this.portComponents[i].toggle = false;

                        if (!setting.cancel) {
                           // this.search();
                            if (setting.type == "student") {
                                this.student = setting.data.zwm + ' - ' + setting.data.xh
                            }else{
                                setting.data.forEach( (v) => {
                                   this.dataList.push(v)
                                })

                            }
                        }
                        return;
                    }
                }
            }

        },
        computed: {
            goodsTotal () { //物品合计
                let number = 0.00;
                for (let i in this.dataList) {
                    if (this.dataList[i].splx == '2') {
                        // console.log(this.dataList[i])
                        number += (this.dataList[i].jg || 0)*1;
                        // number += (this.dataList[i].jcjg || 0) * (this.dataList[i].number || 0) * (this.dataList[i].zk.replace(/%/,"")*1/100 || 0)
                    }
                }
                return number
            },
            courseTotal () { //课时合计
                let number = 0.00;
                for (let i in this.dataList) {
                    if (this.dataList[i].splx == '1') {
                        // number += (this.dataList[i].jg || 0)*1;
                        console.log(122)

                        console.log(this.dataList[i].jcjg*this.dataList[i].sl,)

                        // number += (this.dataList[i].jg || 0) * (this.dataList[i].cns || 0) * (this.dataList[i].zk.replace(/%/,"")*1/100 || 0)
                        number += (this.dataList[i].jcjg || 0) * (this.dataList[i].sl || 0) * (this.dataList[i].zk*1/100 || 0)
                          console.log(number)
                    }
                }
                return number
            },
            totalAmount () {
                // let number = 0;
                // for (let i in this.dataList) {
                //     // number += (this.dataList[i].jcjg || 0) * (this.dataList[i].number || 0) * (this.dataList[i].zk.replace(/%/,"")*1/100 || 0)
                // }
                // console.log(number)
                // return number - (this.ticket?this.ticket:0);
                // return this.goodsTotal + this.courseTotal - this.oldTicket;
                return this.goodsTotal + this.courseTotal;
            },
            xujiao () {

                return this.goodsTotal + this.courseTotal-this.amountReceived;
            },
            xfje(){

                return this.money.xj + this.money.zfbzhje + this.money.wxzhje + this.money.yhzhje
            },
            // sum () {
            //     return this.money.xj*1 + this.money.zfbzhje*1 + this.money.wxzhje*1 + this.money.yhzhje*1
            // },
            accountDisabled () {
                return this.dataList.length > 0 && this.studentid ? false : true
            }
        }
    }
</script>

<style lang="less" scoped>
    .index-page {
        font-size:12px;

        .admin-table {
            margin-top:10px;
        }

        .name {
          margin-top: 20px;
            .small-input{
                width:150px;
            }
        }

        .state {
            width:100%;
            margin-top:20px;
            text-align: center;

            .left,.right {
                float: left;
                width: 50%;
            }

            h3 {
                // width: 350px;
                text-align: center;
                color:#333;
            }
            .item {
                width: 350px;
                margin:10px 0;
            }

            .amount {
                font-size:30px;
                margin-top:20px;
            }

            .left span{
                display: inline-block;
                width:100px;
                text-align: right;
            }
            .right {
                text-align: left;
            }

            .right .item {
                width:500px;

                .title {
                    width: 300px;
                }

                .content {
                    width:150px;
                }
            }
            .right .xj {
                width:400px;
            }
        }

        .content-input {
            height:20px;
        }

        .submit {
            width:120px;
            display: block;
            margin:10px auto;
        }

        .item-input {
            width: 200px;
        }

        .total {
            text-align: right;
            margin:10px 0;
            span{
                margin:0 10px;
            }
        }

        article {
            margin:40px 0;
            margin-top:20px;

            p{
                background: #fcf4eb;
                padding:10px 5px;
            }
            input {
                border:none;
                outline: none;
                background: transparent;
                border-bottom:1px solid #ccc;
            }
            div {
                background: rgba(252,244,235,0.7);
                padding:10px 5px;
            }
            .space-between {
                padding:10px 0;
                display: block;
            }
        }

        .settlement {
            p{
                background: #fcf4eb;
            }
            div {
                background: rgba(252,244,235,0.7);
            }
        }

        .receiving-account {
            p{
                background: #F1FDFC;
            }
            div {
                background: rgba(241,253,252,0.7);
            }
        }

        .other-information {
            p{
                background: #e5e9f2;
            }
            div {
                background: rgba(229,233,242,0.7);
            }
        }
    }
</style>
